/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.workforce.util;

import java.io.UnsupportedEncodingException;

/**
 *
 * @author neo
 */
public class Unicode {

    static String prefix = "&#x0E";
    static String prefix2 = "&#x0E";
    static char ciis[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    public Unicode() {
    }

    //!-- 08-3715-5130
    //-- chajang@hotmail.com
    public static String encoding(String s) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            buf.append(encode(c));
        }
        return buf.toString();
    }

    public static String encode(String s) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            buf.append(encoding(c));
        }
        return buf.toString();
    }

    public static String encoding(char c) {
        //if( c == ' ' ) return "+";
        if ((int) c < 3585 || (int) c > 3675) {
            return "&#x0E" + Integer.toHexString((int) c) + ";";
        }
        for (int i = 3585, j = 0, k = 1; i < 3676; i++) {
            if (c == ((char) i)) {
                return prefix2 + ciis[j] + ciis[k] + ";";
            }
            if (k == (ciis.length - 1)) {
                k = 0;
                j++;
            } else {
                k++;
            }

            if (j == (ciis.length - 1)) {
                j = 0;
            }
        }
        return c + "";
    }

    public static String encode(char c) {
        if ((int) c < 3585 || (int) c > 3675) {
            return c + "";
        }
        for (int i = 3585, j = 0, k = 1; i < 3676; i++) {
            if (c == ((char) i)) {
                return prefix + ciis[j] + ciis[k] + ";";
            }
            if (k == (ciis.length - 1)) {
                k = 0;
                j++;
            } else {
                k++;
            }

            if (j == (ciis.length - 1)) {
                j = 0;
            }
        }
        return c + "";
    }

    public static void table() {
        for (int i = 3585, j = 0, k = 1; i < 3676; i++) {
            System.out.println(prefix2 + ciis[j] + ciis[k] + "|" + (char) i);
            if (k == (ciis.length - 1)) {
                k = 0;
                j++;
            } else {
                k++;
            }

            if (j == (ciis.length - 1)) {
                j = 0;
            }
        }
    }

    public static String decoder(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '+':
                    sb.append(' ');
                    break;
                case '%':
                    try {
                        if (s.charAt(i + 1) == 'u') {
                            System.out.println("[" + s.substring(i + 2, i + 6) + "]");
                            sb.append((char) Integer.parseInt(s.substring(i + 2, i + 6), 16));
                            i += 5;
                        } else {
                            sb.append((char) Integer.parseInt(s.substring(i + 1, i + 3), 16));
                            i += 2;
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    break;
                default:
                    sb.append(c);
                    break;
            }
        }
        String result = sb.toString();
        //System.out.println("ans[" + result + "]");
        try {
            byte[] inputBytes = result.getBytes("UTF-8");
            result = new String(inputBytes);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
//	public static void main(String[] args) throws Exception {
//		Unicode u = new Unicode();
//		//u.readFile("/root/readme1.txt");
//        //u.encode("เก้าอี้โยกหลังพิงแกะสลัก");
//		//System.out.println(Unicode.decoder(en));
//		System.out.println(Unicode.encoding("สยามพาลากอน"));
//		//System.out.println(Integer.toHexString(3585));
//	}
}
